import pygame
import sys
from pygame.locals import *
import time
import pymunk as pm
import math
import os
from utils import distance, vector, unit_vector


def handle_mouse_input(event, mouse_pressed, mouse_distance, rope_lenght, sling_x, sling_y, sling2_x, sling2_y, bird, birds, level, t1, mouse_x, mouse_y, x_mouse, y_mouse):
    """处理鼠标输入"""
    if event.type == pygame.MOUSEBUTTONDOWN:
        if event.button == 1:
            mouse_pressed = True
            mouse_distance = distance(mouse_x, mouse_y, bird.x, bird.y)
            if mouse_distance <= rope_lenght:
                mouse_pressed = True
            else:
                mouse_pressed = False
    
    if event.type == pygame.MOUSEBUTTONUP:
        if event.button == 1:
            if mouse_pressed:
                # 发射小鸟
                bird_x, bird_y = bird.x, bird.y
                if mouse_distance > rope_lenght:
                    mouse_distance = rope_lenght
                if mouse_x < sling_x + 5:
                    bird.body.velocity = (-1 * mouse_distance * 53, 0)
                else:
                    xo = sling_x
                    yo = sling_y
                    x = mouse_x
                    y = mouse_y
                    if mouse_distance > rope_lenght:
                        mouse_distance = rope_lenght
                    v = vector((xo, yo), (x, y))
                    uv = unit_vector(v)
                    uv1 = uv[0]
                    uv2 = uv[1]
                    bird.body.velocity = (-1 * mouse_distance * uv1 * 8, -1 * mouse_distance * uv2 * 8)
                
                # 移除当前小鸟，准备下一只
                if level.number_of_birds > 0:
                    level.number_of_birds -= 1
                    t1 = time.time()
                    if level.number_of_birds > 0:
                        birds[level.number_of_birds - 1].x = 100
                        birds[level.number_of_birds - 1].y = 100
            mouse_pressed = False
    
    if event.type == pygame.MOUSEMOTION:
        mouse_x, mouse_y = pygame.mouse.get_pos()
        mouse_distance = distance(mouse_x, mouse_y, bird.x, bird.y)
        if mouse_pressed and mouse_distance <= rope_lenght:
            if mouse_x < sling_x + 5:
                bird.x = sling_x + 5
            else:
                bird.x = mouse_x
            if mouse_y < sling2_y + 5:
                bird.y = sling2_y + 5
            else:
                bird.y = mouse_y
            x_mouse, y_mouse = pygame.mouse.get_pos()
    
    return mouse_pressed, mouse_distance, t1, mouse_x, mouse_y, x_mouse, y_mouse


def handle_keyboard_input(event, running, space, level, birds, pigs, columns, beams):
    """处理键盘输入"""
    if event.type == KEYDOWN:
        if event.key == K_ESCAPE:
            running = False
        if event.key == K_SPACE:
            # 重新开始游戏
            space = pm.Space()
            space.gravity = (0.0, -700.0)
            level = Level(pigs, columns, beams, space)
            level.load_level()
            level.number_of_birds = 4
            birds = []
            birds.append(Bird(100, 100, space))
            birds.append(Bird(70, 100, space))
            birds.append(Bird(40, 100, space))
            birds.append(Bird(10, 100, space))
    
    return running, space, level, birds